package com.tj.bo.trade.server.service.rule;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.tj.bo.trade.client.enums.OrderStatusEnum;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 青铜 连续2天每天的胜率都大于55%
 * Author: Amarendar
 * Date: 2020/8/25 上午11:01
 */
@Service
public class TradeBronzeRule1 extends BaseTradeRule {

    private static final String RULE_NAME = "连续2天每天的胜率都大于55%";

    private static final Integer DEFAULT_PRIORITY = 3;

    @Override
    public void checkRouterRule(RouterContext routerContext, List<RuleResultVO> ruleResultVOList) {
        Date endDate = DateUtil.getPatternDate(new Date(), "yyyy-MM-dd");
        Date startDate = DateUtil.addDate(endDate, -2);

        List<BinaryTradeEntity> binaryTradeEntityList = binaryTradeDao.selectsBinaryTradeListByTime(routerContext.getUserCode(), routerContext.getProductCode(), OrderStatusEnum.CLOSING.getCode(), TradeTypeEnum.REAL_TRADE.getCode(), startDate, endDate);
        if (CollectionUtils.isEmpty(binaryTradeEntityList) || binaryTradeEntityList.size() < redisConfigUtil.getChangeOddsUserTradeNumbers()) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("订单数过少"));
            return;
        }

        Date referenceDate = DateUtil.addDate(endDate, -1);
        List<BinaryTradeEntity> list1 = new ArrayList<>();//前天
        List<BinaryTradeEntity> list2 = new ArrayList<>();//昨天
        for (BinaryTradeEntity binaryTradeEntity : binaryTradeEntityList) {
            if (binaryTradeEntity.getAmount().compareTo(binaryTradeEntity.getProfitAmount()) == 0) {
                continue;
            }
            if (binaryTradeEntity.getGmtCreate().getTime() < referenceDate.getTime()) {
                list1.add(binaryTradeEntity);
            } else {
                list2.add(binaryTradeEntity);
            }
        }
        if (list1.size() == 0 || list2.size() == 0) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("不满足连续两日的条件"));
            return;
        }

        long num1 = list1.stream().filter(s -> s.getProfitAmount().compareTo(s.getAmount()) > 0).count();
        long num2 = list2.stream().filter(s -> s.getProfitAmount().compareTo(s.getAmount()) > 0).count();

        if ((double) num1 / list1.size() > 0.55 && (double) num2 / list2.size() > 0.55) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), RULE_NAME);
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.valueOf(-0.05));
            ruleResultVO.setMessage("连续2天每天的胜率都大于55%");
            ruleResultVOList.add(ruleResultVO);
        } else {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("连续2天每天的胜率都大于55%-条件不满足"));
        }
    }
}
